package com.example.wanglei.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView;
import android.widget.Toast;
import android.view.View;
import android.content.Context;
import android.os.Handler;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Message;
import android.content.Intent;

import com.example.wanglei.myapplication.PostUtils;
import com.example.wanglei.myapplication.DetailActivity;

import java.io.Console;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private Context mContext;
    private String result = "";
    private String news_no = "";
    private String[] strs;
    private String[] newsNos;

    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
//            Toast.makeText(mContext, result, Toast.LENGTH_SHORT).show();

//            List<String> list=new ArrayList<String>();
//
//            /**
//             * 这里需要分析服务器回传的json格式数据，
//             */
//            JSONObject jsonObject = new JSONObject(result).getJSONObject("params");
//            JSONArray jsonArray = jsonObject.getJSONArray("list");
//            for(int i=0;i<jsonArray.length();i++){
//                JSONObject jsonObject2 = (JSONObject)jsonArray.opt(i);
//                list.add(jsonObject2.getString("sTitle"));
//            }
//
//            int size=list.size();
//            String[] strs = (String[])list.toArray(new String[size]);


        };

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mContext = MainActivity.this;

        new Thread() {
            public void run() {
                result = PostUtils.LoginByPost("http://47.97.107.221:8080/cland-websiteFront/news/queryNewsList.htm?iType=2");
//                handler.handleMessage(result);

                //更新主线程UI
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        /**
                         //             * 这里需要分析服务器回传的json格式数据，
                         //             */
                        List<String> list=new ArrayList<String>();
                        List<String> noList=new ArrayList<String>();

                        try {
                            JSONObject jsonObject = new JSONObject(result).getJSONObject("params");
                            JSONArray jsonArray = jsonObject.getJSONArray("list");
                            for(int i=0;i<jsonArray.length();i++){
                                JSONObject jsonObject2 = (JSONObject)jsonArray.opt(i);
                                list.add(jsonObject2.getString("sTitle"));
                                noList.add(jsonObject2.getString("sNewsNo"));
                            }

                            int size=list.size();

                            strs = (String[])list.toArray(new String[size]);
                            newsNos = (String[])noList.toArray(new String[size]);

                            ArrayAdapter<String> adapter = new ArrayAdapter<String>
                                    (mContext,android.R.layout.simple_expandable_list_item_1,strs);
                            ListView list_test = (ListView) findViewById(R.id.list_test);
                            list_test.setAdapter(adapter);
                            list_test.setOnItemClickListener(MainActivity.this);

                        } catch (Exception e) {
//                            Log.e("tag", e.toString());
                        }

                    }
                });
            };

        }.start();

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//        Toast.makeText(mContext,"你点击了第" + position + "项",Toast.LENGTH_SHORT).show();

        news_no = newsNos[position];
        Intent intent =new Intent(MainActivity.this,DetailActivity.class);
        intent.putExtra("news_no", news_no);
        startActivity(intent);
    }
}

